/**
 * 
 */
package es.ats.codington.festival.junit;


import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.daos.PlaceDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;

import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.utils.FERSDataConnection;


/**
 * @author Codington_RED
 * 
 */
public class TEventDAO extends TestCase{
	
	
	Connection conn;
	EventDAOImpl eventDAO;
	PlaceDAOImpl placeDAO;
	Event ev1, event, event2;
	Place place1, place;

	/**
	 * Method to create a new event for the test
	 * @return ev1
	 */
	
	public Event createEvent(){
		
		ev1 = new Event();
		
		ev1.setName("concierto de los rolling");
		ev1.setDescription("concierto de la gira 2015");
		ev1.setPlace("Estadio Vicente Calderon");
		ev1.setStartEvent("20:00");
		ev1.setEndEvent("22:00");
		ev1.setDayEvent("Viernes");
		ev1.setEventType("Concierto");
		ev1.setSeatsAvailable(20000);
		
		
		
		return ev1;
	}
	 
	 
	/**
	 * Method to create a new place for the test
	 * @return place1
	 */
	public Place createPlace() {
		place1 = new Place();
		
		place1.setNamePlace("Codington Arena");
		place1.setCapacityPlace(100000);
		place1.setPlaceDescription("Footbal Stadium");
		place1.setZonePlace("NORTH");
		place1.setImagePlace("");
		
		
		return place1;
	}



	/**
	 * SetUp of the test
	 * @throws Exception
	 */
	@Before
	public void setUp() throws Exception {
		super.setUp();
		conn = FERSDataConnection.getConnection();
		eventDAO = new EventDAOImpl(conn);
		placeDAO = new PlaceDAOImpl(conn);
		
	}

	/**
	 * TearDown of the test
	 * @throws Exception
	 */
	
	@After
	public void tearDown() throws Exception {
		super.tearDown();
		if(!conn.isClosed()){
			FERSDataConnection.rollback(conn);
			FERSDataConnection.closeConnection(conn);
		}
	}

	/**
	 * Test for the InsertEvent method
	 */
	@Test
	public void testInsertEvent(){
		boolean affectedRows;
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		affectedRows = eventDAO.insertEvent(event);
		assertTrue(affectedRows);
		
		try {
			FERSDataConnection.closeConnection(conn);
		}
		catch (DAOException ex){}
		
		

	}

	/**
	 * Test for the GetEventById method
	 */
	@Test
	public void testGetEventById(){
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
		event2 = new Event();
		event2 = eventDAO.getEvent(event.getEventID());
		
		assertEquals(event.getEventID(),event2.getEventID());
		
		try {
			FERSDataConnection.closeConnection(conn);
		}
		catch (DAOException ex){}
	
		
	}
	

	/**
	 * Test for the UpdateEvent method
	 */
	@Test
	public void testUpdateEvent(){
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
		event.setName("concierto de blur");
		event.setDescription("concierto de la gira 2014");
		event.setPlace("Estadio Vicente");
		event.setStartEvent("21:00");
		event.setEndEvent("23:00");
		event.setDayEvent("Viernes");
		event.setEventType("Concierto");
		event.setSeatsAvailable(30000);
		event.setIdPlace(place.getId());
		
		eventDAO.updateEvent(event);
		assertEquals(event.getName(), eventDAO.getEvent(event.getEventID()).getName());
		
		try {
			FERSDataConnection.closeConnection(conn);
		}
		catch (DAOException ex){}
		
	}
	
	

	/**
	 * Test for the ShowAllEvents method
	 */
	@Test
	public void testShowAllEvents(){
		boolean theresEvents = false;
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
		List<Event> events = new ArrayList <Event>();
		events = eventDAO.showAllEvents();
		if (events.size() > 0){
			theresEvents=true;
		}
		
		assertTrue(theresEvents);
		
		try {
			FERSDataConnection.closeConnection(conn);
		}
		catch (DAOException ex){}
		
	}
	
	/**
	 * Test for the ShowEventByName method
	 */
	@Test
	public void testShowEventsByName(){
		
		place = new Place();
		place = this.createPlace();
		
		placeDAO.insertPlace(place, "Stadium");
		
		event = new Event();
		event = this.createEvent();
		event.setIdPlace(place.getId());
		
		eventDAO.insertEvent(event);
		
		List<Event> events = new ArrayList <Event>();
		
		events = eventDAO.showEvents(event);
		
		assertEquals(events.size(),1);
		
		try {
			FERSDataConnection.closeConnection(conn);
		}
		catch (DAOException ex){}
		
	
	}
	
}

